<template>
  <div>
    <FapiaoPage
      ref="refForm"
      :editable="false"
      :fields="fields"
      :view-info="viewInfo"
      :data-info="{ record }"
    />
  </div>
</template>

<script>
function sleep(millisecond) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve()
    }, millisecond)
  })
}

import api from '@/odoorpc'

import FapiaoPage from '../fapiao_page.vue'

export default {
  name: 'FpFormView',
  components: { FapiaoPage },
  mixins: [],

  props: {},

  data() {
    return {
      actionId: undefined,
      res_id: undefined,

      view: undefined, // 对象, 接口函数的入口
      viewInfo: {},
      fields: {}, // 模型的字段信息

      record: {}
    }
  },

  computed: {},

  watch: {},

  async created() {},

  mounted() {},

  methods: {
    async load_relation() {
      let relations = {}

      // 自定义 a-form-model
      if (this.$refs.refForm) {
        if (this.$refs.refForm.load_relation) {
          const rels = await this.$refs.refForm.load_relation()
          relations = { ...relations, ...(rels || {}) }
        }
      }

      this.view.load_relations_done(relations)
    },

    async load_relation_data(for_new) {
      // for_new=true, only for wizard form
      if (this.$refs.refForm) {
        if (this.$refs.refForm.load_relation_data) {
          const res = await this.$refs.refForm.load_relation_data(for_new)
          // TODO: values_write
        }
      }
    },

    async init(actionId, res_id) {
      console.log('init')

      this.actionId = actionId
      this.res_id = res_id

      const view = api.env.formview(actionId)
      this.view = view
      // this.buttons = view.buttons
      this.fields = await view.load_fields()
      this.viewInfo = view.view_info
      await sleep(10)

      await this.load_relation()

      const record = await view.read(res_id)
      this.record = record

      await sleep(10)
      await this.load_relation_data()
    }
  }
}
</script>

<style type="text/css"></style>
